package com.hzg.stream.sideoutput;

import com.hzg.util.CommonUtil;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @Author: huangzhigao
 * @Date: 2022/4/19 16:39
 */
public class SideOutPutDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = CommonUtil.getEnv();
        env.setParallelism(1);
        DataStreamSource<Long> source = env.generateSequence(1, 10);
        OutputTag<String> outputTag1 = new OutputTag<String>("tag1") {
        };
        OutputTag<String> outputTag2 = new OutputTag<String>("tag2") {
        };
        SingleOutputStreamOperator<Long> processStream = source.process(new ProcessFunction<Long, Long>() {
            @Override
            public void processElement(Long value, Context ctx, Collector<Long> out) throws Exception {
                String res = value + "-" + value;
                out.collect(value);
                if (value % 2 == 0) {
                    ctx.output(outputTag1, res);
                } else {
                    ctx.output(outputTag2, res);
                }
            }
        });
//        processStream.print();
        DataStream sideOutput = processStream.getSideOutput(outputTag1);
        sideOutput.print();
        env.execute();

    }
}
